package com.zq.it.action.admin;


import java.util.List;

import javax.annotation.Resource;

import com.zq.it.action.BaseAction;
import com.zq.it.pojo.Chapter;
import com.zq.it.service.admin.ChapterService;
import com.zq.it.tool.Constant;
import com.zq.it.tool.EtyLsName;
import com.zq.it.tool.FileName;
import com.zq.it.tool.FileType;
import com.zq.it.tool.HeadTitle;

import net.sf.json.JSONArray;

public class ChapterAction extends BaseAction {
	
	private static final long serialVersionUID = 1L;
	@Resource
	private ChapterService chapterService;
	private Chapter chapter=null;	
	private int [] chapterGroupId;
	
	public void searchChapterList(){
		  List list=chapterService.getAllChapterList();
		  uploadList(EtyLsName.CHAPTER_LS.name(),list);
		  print(JSONArray.fromObject(list,Constant.getJcf(new String[]{"chapters","createDate","state"})).toString(),"UTF-8","APPLICATION/JSON");
	}
	public void searchChapterListByHQL(){
		 List list=chapterService.getChapterListBySQL(chapter);
		 uploadList(EtyLsName.CHAPTER_LS.name(),list);
		 print(JSONArray.fromObject(list,Constant.getJcf(new String[]{"chapters","createDate","state"})).toString(),"UTF-8","APPLICATION/JSON");
	}
	
	public void searchCourseNameAndId(){
		print(toJSON(chapterService.getCourseNameAndId()).toString(),"UTF-8","APPLICATION/JSON");
	}
	
	public void deleteChapterGroupId(){		
		 chapterService.removeAllChapter(chapterGroupId);
		 print("已删除"+chapterGroupId.length+"条记录！","UTF-8","text/html");
	}
	public void addChapterPo(){
		chapterService.addChapter(chapter);
		print("<script>parent.closeWindow('已添加');</script>","UTF-8","text/html");	
	}
	public void modifyChapterPo(){
		chapterService.renewalChapter(chapter);
		print("<script>parent.closeWindow('已修改！');</script>","UTF-8","text/html");	
	}
	public void importChapterList(){	
		String msgStr="";
		List<Chapter> list=chapterService.importChapterList(getUpload());
		if(list.size()>0){
		 getRequest().getSession().setAttribute("existChapterList",list);	
		 msgStr="existList";
		}else{
			msgStr="已导入成功!";
		}
		print("<script>parent.importBackResult('"+msgStr+"','webpage/adminpage/chapter/existChapter.jsp');</script>","UTF-8","TEXT/HTML");
	}
	public void exportChapterList(){				
		download(chapterService.exportChapterList(HeadTitle.CN_CHAPTERHEAD.getArray(),(List<Chapter>)getHttpSession().getAttribute(EtyLsName.CHAPTER_LS.name())),FileName.CHAPTER_LIST_NAEM.toString()+FileType.XLSX);
	}
	public void addChapter(){
		chapterService.addChapter(chapter);		
	}
	
	public Chapter getChapter(){
		return chapter;
	}
	public void setChapter(Chapter chapter){
		this.chapter = chapter;
	}
	public int[] getChapterGroupId() {
		return chapterGroupId;
	}
	public void setChapterGroupId(int[] chapterGroupId) {
		this.chapterGroupId = chapterGroupId;
	}
	public void deleteChapter(){
		chapterService.removeChapter(chapter);
	}
	
	
}
